package main;
import (

	"fmt"
	"time"
)
var taskList [10] chan string //有十个通道的数组
func workThread(id int ){
	
	fmt.Println("thread id=",id)
	for true {
		select {
			case i,ok := <- taskList[id]:
				{
					if(ok){
						fmt.Println("process task  id:",i)
					}else {
						fmt.Println(" workThread chan closed ")						
						goto end
					}
				}
			default:
			{
				
				fmt.Printf("select enter default id = %d\n",id)
				time.Sleep(300 * time.Millisecond )
			}	
		}

	}
	end:
		fmt.Println("工作线程结束 id= ",id)
		defer wg.Done();
	
}

func goThreadPoll(pollsize int){
	

	for i := 0; i < pollsize; i++ {
		wg.Add(1)
		go workThread(i);
	}

}	

func makeTask(pollsize int){
	
	for j:=0;j<=3;j++ {
		fmt.Printf("**************************round %d*************************\n",j)
		for i := 0; i < pollsize ; i++ {
			str := fmt.Sprintf("new task %d",j)
			taskList[i] <- str		
				
		}
		time.Sleep(100 * time.Millisecond * 10)
		
	}
	//跑四圈之后关闭所有通道 ，看工作线程表现
	for i := 0; i < pollsize ; i++ {		
		close(taskList[i])			
	}
	wg.Done()

}
func test03(){
	pollsize := 10
	//初始化通道及其缓冲区大小
	for i := 0; i < pollsize ; i++ {
		taskList[i] = make(chan string, 100)
	}

	
	wg.Add(1)
	go makeTask(pollsize)

	goThreadPoll(pollsize)
}
